<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Amitav Roy
 * Date: 1/26/13
 * Time: 11:00 PM
 * To change this template use File | Settings | File Templates.
 */
class Node_model extends CI_Model {

  protected $node_fields;
  protected $cache_time = 604800; // a week

  public function __construct() {
    parent::__construct();
  }

  // load a single node
  public function node_load($nid) {
    // load the node based on the node id
    return $this->build_node($nid);
  }

  // load multiple node
  public function node_load_multiple($nids) {
    if (is_array($nids)) {
      foreach ($nids as $nid) {
        // loading the node
        $nodes[] = $this->node_load($nid);
      }

      // returning the set of nodes
      return $nodes;
    }
    else {
      // if it's not an array, but single
      return $this->node_load($nids);
    }
  }

  // building the node from different tables
  protected function build_node($nid) {
    $this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
    if ($node = $this->cache->get('node-'.$nid)) {
      return $node;
    }

    $this->node_fields = array(
      'n.title', 'n.type', 'n.created', 'n.changed', 'n.nid as nodeid'
    );

    $this->db->where('n.status', 1); // published articles only

    $this->get_node_body($nid); // join table for node body and summary
    $this->get_node_preview_image($nid); // join table for preview image

    // selecting the fields
    $this->db->select(implode(',', $this->node_fields));
    $this->db->where('n.nid', $nid); // adding the nid as where clause

    $query = $this->db->get('node as n');
    //print $this->db->last_query();

    $result = $query->result_array();

    // attach the tags
    $result = $this->attach_tags($result);

    // attach node alias
    $result = $this->attach_url_alias($result);

    // attach node keywords
    $result = $this->attach_metatags($result);

    // setting the cache
    $this->cache->file->save('node-'.$nid, $result, $this->cache_time);

    return $result;
  }

  // join the node body and summary
  protected function get_node_body() {
    array_push($this->node_fields, 'fb.body_value as body', 'fb.body_summary as teaser');
    $this->db->join('field_revision_body as fb','fb.entity_id = n.nid');
  }

  // get the preview image for the node
  protected function get_node_preview_image() {
    array_push($this->node_fields,'fm.uri as uri');
    $this->db->join('field_revision_field_preview_image as prvimg', 'n.nid=prvimg.entity_id'); // field data
    $this->db->join('file_managed as fm', 'fm.fid=prvimg.field_preview_image_fid'); // uri from field managed
  }

  // attach the url alias of the node
  protected function attach_url_alias($node) {
    $node[0]['alias'] = $this->alias_model->get_alias($node[0]['nodeid'],'node');
    return $node;
  }

  // attach the tags to the node
  protected function attach_tags($node) {
    // loading the model
    $this->load->model('taxonomy/taxonomy_model', 'tax');

    // attaching the tags array to node
    $node[0]['tags'] = $this->tax->get_term($node[0]['nodeid']);

    return $node;
  }

  protected function attach_metatags($node) {
    // loading the model
    $this->load->model('metatags/metatags_model', 'metatags');

    // attaching the meta tags to the node
    $node[0]['keywords'] = $this->metatags->get_node_tags($node[0]['nodeid']);

    return $node;
  }
}
